home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / uip / ucbmail / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-06-03  |  6.5 KB  |  354 lines

  1. /*
  2.  *  M A I N . C 
  3.  *
  4.  *  EE/CIS Computer Lab
  5.  *  Department of Computer and Information Sciences
  6.  *  Department of Electrical Engineering
  7.  *  University of Delaware
  8.  *
  9.  *  REVISION HISTORY:
  10.  *
  11.  *  $Revision: 1.3 $
  12.  *
  13.  *  $Log:    main.c,v $
  14.  * Revision 1.3  85/11/20  12:23:08  galvin
  15.  * Added call to mmdf_init.
  16.  * 
  17.  * Revision 1.2  85/11/18  15:29:35  galvin
  18.  * Added comment header for revision history.
  19.  * 
  20.  *
  21.  */
  22.  
  23. /*
  24.  * Copyright (c) 1980 Regents of the University of California.
  25.  * All rights reserved.  The Berkeley software License Agreement
  26.  * specifies the terms and conditions for redistribution.
  27.  */
  28.  
  29. #ifndef lint
  30. char *copyright =
  31. "@(#) Copyright (c) 1980 Regents of the University of California.\nAll rights reserved.\n";
  32. #endif not lint
  33.  
  34. #ifndef lint
  35. static char *sccsid = "@(#)main.c    5.2 (Berkeley) 6/21/85";
  36. #endif not lint
  37.  
  38. #include "./rcv.h"
  39. #include <sys/stat.h>
  40.  
  41. /*
  42.  * Mail -- a mail program
  43.  *
  44.  * Startup -- interface with user.
  45.  */
  46.  
  47. jmp_buf    hdrjmp;
  48.  
  49. /*
  50.  * Find out who the user is, copy his mail file (if exists) into
  51.  * /tmp/Rxxxxx and set up the message pointers.  Then, print out the
  52.  * message headers and read user commands.
  53.  *
  54.  * Command line syntax:
  55.  *    Mail [ -i ] [ -r address ] [ -h number ] [ -f [ name ] ]
  56.  * or:
  57.  *    Mail [ -i ] [ -r address ] [ -h number ] people ...
  58.  */
  59.  
  60. main(argc, argv)
  61.     char **argv;
  62. {
  63.     register char *ef;
  64.     register int i, argp;
  65.     int mustsend, hdrstop(), (*prevint)(), f;
  66.     struct sgttyb tbuf;
  67.  
  68.     /*
  69.      * Set up the MMDF environment.
  70.      */
  71.     
  72.     mmdf_init(argv[0]);
  73.  
  74. #ifdef signal
  75.     Siginit();
  76. #endif
  77.  
  78.     /*
  79.      * Set up a reasonable environment.  We clobber the last
  80.      * element of argument list for compatibility with version 6,
  81.      * figure out whether we are being run interactively, set up
  82.      * all the temporary files, buffer standard output, and so forth.
  83.      */
  84.  
  85.     argv[argc] = (char *) -1;
  86. #ifdef    GETHOST
  87.     inithost();
  88. #endif    GETHOST
  89.     mypid = getpid();
  90.     intty = isatty(0);
  91.     outtty = isatty(1);
  92.     if (outtty) {
  93.         gtty(1, &tbuf);
  94.         baud = tbuf.sg_ospeed;
  95.     }
  96.     else
  97.         baud = B9600;
  98.     image = -1;
  99.  
  100.     /*
  101.      * Now, determine how we are being used.
  102.      * We successively pick off instances of -r, -h, -f, and -i.
  103.      * If called as "rmail" we note this fact for letter sending.
  104.      * If there is anything left, it is the base of the list
  105.      * of users to mail to.  Argp will be set to point to the
  106.      * first of these users.
  107.      */
  108.  
  109.     ef = NOSTR;
  110.     argp = -1;
  111.     mustsend = 0;
  112.     if (argc > 0 && **argv == 'r')
  113.         rmail++;
  114.     for (i = 1; i < argc; i++) {
  115.  
  116.         /*
  117.          * If current argument is not a flag, then the
  118.          * rest of the arguments must be recipients.
  119.          */
  120.  
  121.         if (*argv[i] != '-') {
  122.             argp = i;
  123.             break;
  124.         }
  125.         switch (argv[i][1]) {
  126.         case 'r':
  127.             /*
  128.              * Next argument is address to be sent along
  129.              * to the mailer.
  130.              */
  131.             if (i >= argc - 1) {
  132.                 fprintf(stderr, "Address required after -r\n");
  133.                 exit(1);
  134.             }
  135.             mustsend++;
  136.             rflag = argv[i+1];
  137.             i++;
  138.             break;
  139.  
  140.         case 'T':
  141.             /*
  142.              * Next argument is temp file to write which
  143.              * articles have been read/deleted for netnews.
  144.              */
  145.             if (i >= argc - 1) {
  146.                 fprintf(stderr, "Name required after -T\n");
  147.                 exit(1);
  148.             }
  149.             Tflag = argv[i+1];
  150.             if ((f = creat(Tflag, 0600)) < 0) {
  151.                 perror(Tflag);
  152.                 exit(1);
  153.             }
  154.             close(f);
  155.             i++;
  156.             break;
  157.  
  158.         case 'u':
  159.             /*
  160.              * Next argument is person to pretend to be.
  161.              */
  162.             if (i >= argc - 1) {
  163.                 fprintf(stderr, "Missing user name for -u\n");
  164.                 exit(1);
  165.             }
  166.             strcpy(myname, argv[i+1]);
  167.             i++;
  168.             break;
  169.  
  170.         case 'i':
  171.             /*
  172.              * User wants to ignore interrupts.
  173.              * Set the variable "ignore"
  174.              */
  175.             assign("ignore", "");
  176.             break;
  177.  
  178.         case 'd':
  179.             debug++;
  180.             break;
  181.  
  182.         case 'h':
  183.             /*
  184.              * Specified sequence number for network.
  185.              * This is the number of "hops" made so
  186.              * far (count of times message has been
  187.              * forwarded) to help avoid infinite mail loops.
  188.              */
  189.             if (i >= argc - 1) {
  190.                 fprintf(stderr, "Number required for -h\n");
  191.                 exit(1);
  192.             }
  193.             mustsend++;
  194.             hflag = atoi(argv[i+1]);
  195.             if (hflag == 0) {
  196.                 fprintf(stderr, "-h needs non-zero number\n");
  197.                 exit(1);
  198.             }
  199.             i++;
  200.             break;
  201.  
  202.         case 's':
  203.             /*
  204.              * Give a subject field for sending from
  205.              * non terminal
  206.              */
  207.             if (i >= argc - 1) {
  208.                 fprintf(stderr, "Subject req'd for -s\n");
  209.                 exit(1);
  210.             }
  211.             mustsend++;
  212.             sflag = argv[i+1];
  213.             i++;
  214.             break;
  215.  
  216.         case 'f':
  217.             /*
  218.              * User is specifying file to "edit" with Mail,
  219.              * as opposed to reading system mailbox.
  220.              * If no argument is given after -f, we read his
  221.              * mbox file in his home directory.
  222.              */
  223.             if (i >= argc - 1)
  224.                 ef = mbox;
  225.             else
  226.                 ef = argv[i + 1];
  227.             i++;
  228.             break;
  229.  
  230.         case 'n':
  231.             /*
  232.              * User doesn't want to source /usr/lib/Mail.rc
  233.              */
  234.             nosrc++;
  235.             break;
  236.  
  237.         case 'N':
  238.             /*
  239.              * Avoid initial header printing.
  240.              */
  241.             noheader++;
  242.             break;
  243.  
  244.         case 'v':
  245.             /*
  246.              * Send mailer verbose flag
  247.              */
  248.             assign("verbose", "");
  249.             break;
  250.  
  251.         case 'I':
  252.             /*
  253.              * We're interactive
  254.              */
  255.             intty = 1;
  256.             break;
  257.  
  258.         default:
  259.             fprintf(stderr, "Unknown flag: %s\n", argv[i]);
  260.             exit(1);
  261.         }
  262.     }
  263.  
  264.     /*
  265.      * Check for inconsistent arguments.
  266.      */
  267.  
  268.     if (ef != NOSTR && argp != -1) {
  269.         fprintf(stderr, "Cannot give -f and people to send to.\n");
  270.         exit(1);
  271.     }
  272.     if (mustsend && argp == -1) {
  273.         fprintf(stderr, "The flags you gave make no sense since you're not sending mail.\n");
  274.         exit(1);
  275.     }
  276.     tinit();
  277.     input = stdin;
  278.     rcvmode = argp == -1;
  279.     if (!nosrc)
  280.         load(MASTER);
  281.     load(mailrc);
  282.     if (argp != -1) {
  283.         mail(&argv[argp]);
  284.  
  285.         /*
  286.          * why wait?
  287.          */
  288.  
  289.         exit(senderr);
  290.     }
  291.  
  292.     /*
  293.      * Ok, we are reading mail.
  294.      * Decide whether we are editing a mailbox or reading
  295.      * the system mailbox, and open up the right stuff.
  296.      */
  297.  
  298.     if (ef != NOSTR) {
  299.         char *ename;
  300.  
  301.         edit++;
  302.         ename = expand(ef);
  303.         if (ename != ef) {
  304.             ef = (char *) calloc((unsigned) 1,
  305.                          (unsigned) (strlen(ename) + 1));
  306.             strcpy(ef, ename);
  307.         }
  308.         editfile = ef;
  309.         strcpy(mailname, ef);
  310.     }
  311.     if (setfile(mailname, edit) < 0) {
  312.         if (edit)
  313.             perror(mailname);
  314.         else
  315.             fprintf(stderr, "No mail for %s\n", myname);
  316.         exit(1);
  317.     }
  318.     if (!edit && !noheader && value("noheader") == NOSTR) {
  319.         if (setjmp(hdrjmp) == 0) {
  320.             if ((prevint = sigset(SIGINT, SIG_IGN)) != SIG_IGN)
  321.                 sigset(SIGINT, hdrstop);
  322.             announce(1);
  323.             fflush(stdout);
  324.             sigset(SIGINT, prevint);
  325.         }
  326.     }
  327.     if (edit)
  328.         newfileinfo();
  329.     if (!edit && msgCount == 0) {
  330.         printf("No mail\n");
  331.         fflush(stdout);
  332.         exit(0);
  333.     }
  334.     commands();
  335.     if (!edit) {
  336.         sigset(SIGHUP, SIG_IGN);
  337.         sigset(SIGINT, SIG_IGN);
  338.         sigset(SIGQUIT, SIG_IGN);
  339.         quit();
  340.     }
  341.     exit(0);
  342. }
  343.  
  344. /*
  345.  * Interrupt printing of the headers.
  346.  */
  347. hdrstop()
  348. {
  349.  
  350.     fflush(stdout);
  351.     fprintf(stderr, "\nInterrupt\n");
  352.     longjmp(hdrjmp, 1);
  353. }
  354.